package com.xuecheng.filesystem.config;

import com.xuecheng.framework.domain.filesystem.response.FileSystemCode;
import com.xuecheng.framework.exception.ExceptionCast;
import feign.Client;
import org.csource.fastdfs.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
@EnableConfigurationProperties
public class FastDFSConfig {

    @Value("${xuecheng.fastdfs.connect_timeout_in_seconds}")
    private int connTimeOut;
    @Value("${xuecheng.fastdfs.network_timeout_in_seconds}")
    private int httpTimeOut;
    @Value("${xuecheng.fastdfs.charset}")
    private String charset;
    @Value("${xuecheng.fastdfs.tracker_servers}")
    private String trackerServer;

    @Bean
    public StorageClient1 storageClient1(){
        try {
            ClientGlobal.initByTrackers(trackerServer);
            ClientGlobal.setG_charset(charset);
            ClientGlobal.setG_network_timeout(httpTimeOut);
            ClientGlobal.setG_connect_timeout(connTimeOut);
            TrackerClient trackerClient = new TrackerClient();
            TrackerServer trackerServer = trackerClient.getConnection();
            StorageServer storeStorage = trackerClient.getStoreStorage(trackerServer);
            return new StorageClient1(trackerServer, storeStorage);
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionCast.cast(FileSystemCode.FS_INITFASTDFSERROR);
        }
        return null;
    }
}
